//
//  HelpRecordViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/9.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "HelpRecordViewController.h"
#import "HelpRecordTableViewCell.h"
@interface HelpRecordViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UILabel * helpPersonContentLab;//已帮助人数
@property (nonatomic, strong)UILabel * joinDaysContentLab;//已加入天数
@property (nonatomic, strong)UIImageView * imgBGView;
@property (nonatomic, strong)NSMutableArray * cellData;
@end

@implementation HelpRecordViewController(networking)
- (void)helpListData{
    NSDictionary * dic = @{
                           @"userId":[UserModel sharedInstanced].userID,
                           @"cookie":[UserModel sharedInstanced].cookie
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"user/helpList" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"1"]) {
            [self.cellData addObjectsFromArray:responseObject[@"list"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation HelpRecordViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"助人记录";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    
    [self helpListData];//网络接口
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 100/WIDTH_6_SCALE;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        [_tableView registerClass:[HelpRecordTableViewCell class] forCellReuseIdentifier:NSStringFromClass([HelpRecordTableViewCell class])];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableHeaderView = self.headerView;
        _tableView.rowHeight = 39/WIDTH_6_SCALE;
    }return _tableView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 123/WIDTH_6_SCALE)];
        _headerView.backgroundColor = [UIColor whiteColor];
        //背景
        self.imgBGView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90/WIDTH_6_SCALE)];
        self.imgBGView.image = [UIImage imageNamed:@"maself_bg"];
        [_headerView addSubview:self.imgBGView];
        //已帮助人数
        self.helpPersonContentLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 26/WIDTH_6_SCALE, SCREEN_WIDTH/2, 20/WIDTH_6_SCALE)];
        [self label:self.helpPersonContentLab withFont:DEF_FontSize_16 withText:self.helpNum withTextColor:[UIColor whiteColor] withTextAlignment:NSTextAlignmentCenter withView:_headerView withBGColor:[UIColor clearColor]];
        //已帮助人数标题
        UILabel * helpPersonTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, self.helpPersonContentLab.bottom + 15/WIDTH_6_SCALE, SCREEN_WIDTH/2, 13/WIDTH_6_SCALE)];
        [self label:helpPersonTitleLab withFont:DEF_FontSize_14 withText:@"已帮助人数(人)" withTextColor:[UIColor whiteColor] withTextAlignment:NSTextAlignmentCenter withView:self.imgBGView withBGColor:[UIColor clearColor]];
        //已加入天数
        self.joinDaysContentLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, self.helpPersonContentLab.top, SCREEN_WIDTH/2, self.helpPersonContentLab.height)];
        [self label:self.joinDaysContentLab withFont:DEF_FontSize_16 withText:self.joinNum withTextColor:[UIColor whiteColor] withTextAlignment:NSTextAlignmentCenter withView:self.imgBGView withBGColor:[UIColor clearColor]];
        //已加入天数标题
        UILabel * joinDaysTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, helpPersonTitleLab.top, helpPersonTitleLab.width, helpPersonTitleLab.height)];
        [self label:joinDaysTitleLab withFont:DEF_FontSize_14 withText:@"已加入天数(天)" withTextColor:[UIColor whiteColor] withTextAlignment:NSTextAlignmentCenter withView:self.imgBGView withBGColor:[UIColor clearColor]];
        //分割线
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, 31/WIDTH_6_SCALE, 1/WIDTH_6_SCALE, self.imgBGView.height - 52/WIDTH_6_SCALE)];
        lineView.backgroundColor = getColor(@"d8d8d8");
        [self.imgBGView addSubview:lineView];
        //灰色背景
        UIView * sectionBGView = [[UIView alloc] initWithFrame:CGRectMake(0, self.imgBGView.bottom, SCREEN_WIDTH, _headerView.height - self.imgBGView.height)];
        sectionBGView.backgroundColor = getColor(@"f5f5f5");
        [_headerView addSubview:sectionBGView];
        //标题
        for (int i = 0; i<3; i++) {
            UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3 * i, 0, SCREEN_WIDTH/3, sectionBGView.height)];
            titleLab.tag = 4000 + i;
            [self label:titleLab withFont:DEF_FontSize_12 withText:nil withTextColor:getColor(@"9b9b9b") withTextAlignment:NSTextAlignmentCenter withView:sectionBGView withBGColor:[UIColor clearColor]];
        }
        UILabel * helpedNameLab = (UILabel *)[sectionBGView viewWithTag:4000];
        helpedNameLab.text = @"被助人";
        UILabel * diseaseLab = (UILabel *)[sectionBGView viewWithTag:4001];
        diseaseLab.text = @"疾病(名称)";
        UILabel * moneyLab = (UILabel *)[sectionBGView viewWithTag:4002];
        moneyLab.text = @"帮助金额(元)";
    }return _headerView;
}
- (NSMutableArray *)cellData{
    if (!_cellData) {
        _cellData = @[].mutableCopy;
    }return _cellData;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HelpRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HelpRecordTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.cellData[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellData.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
@end
